package com.market.service.admin.impl;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.market.bean.MarketCoupon;
import com.market.bean.MarketCouponExample;
import com.market.bean.MarketCouponUser;
import com.market.bean.MarketCouponUserExample;
import com.market.bean.common.BasePageInfo;
import com.market.bean.common.CommonData;
import com.market.mapper.MarketCouponMapper;
import com.market.mapper.MarketCouponUserMapper;
import com.market.service.admin.AdminCouponService;
import com.market.utils.RandomUtil;
import com.market.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author fan
 * @Description:
 * @since 2022/12/29 14:14
 */
@Service
public class AdminCouponServiceImpl implements AdminCouponService {
    @Autowired
    MarketCouponMapper couponMapper;

    @Autowired
    MarketCouponUserMapper couponUserMapper;


    @Override
    public CommonData<MarketCoupon> list(BasePageInfo info, String name, Short type, Short status) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketCouponExample example = new MarketCouponExample();
        example.setOrderByClause(info.getSort() + " " + info.getOrder());
        MarketCouponExample.Criteria criteria = example.createCriteria();
        if (!StringUtil.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (type != null) {
            criteria.andTypeEqualTo(type);
        }
        if (status != null) {
            criteria.andStatusEqualTo(status);
        }
        criteria.andDeletedEqualTo(false);
        List<MarketCoupon> list = couponMapper.selectByExample(example);
        PageInfo<MarketCoupon> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }

    @Override
    public MarketCoupon create(MarketCoupon coupon) {
        Date date = new Date();
        coupon.setAddTime(date);
        coupon.setUpdateTime(date);
        if (coupon.getType() == 2) {
            coupon.setCode(RandomUtil.couponCode());
        }
        couponMapper.insertSelective(coupon);
        return coupon;
    }

    @Override
    public MarketCoupon update(MarketCoupon coupon) {
        Date date = new Date();
        coupon.setUpdateTime(date);
        couponMapper.updateByPrimaryKeySelective(coupon);
        return coupon;
    }

    @Override
    public void delete(MarketCoupon coupon) {
        coupon.setDeleted(true);
        couponMapper.updateByPrimaryKeySelective(coupon);
    }

    @Override
    public CommonData<MarketCouponUser> listUser(BasePageInfo info, Integer couponId, Integer userId, Short status) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketCouponUserExample example = new MarketCouponUserExample();
        example.setOrderByClause(info.getSort() + " " + info.getOrder());
        MarketCouponUserExample.Criteria criteria = example.createCriteria();
        criteria.andCouponIdEqualTo(couponId);
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (status != null) {
            criteria.andStatusEqualTo(status);
        }
        criteria.andDeletedEqualTo(false);
        List<MarketCouponUser> list = couponUserMapper.selectByExample(example);
        PageInfo<MarketCouponUser> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }

    @Override
    public MarketCoupon read(Integer id) {
        return couponMapper.selectByPrimaryKey(id);
    }
}
